//Source file: D:\\aaa\\com\\ce\\pms\\renovation\\service\\impl\\InspectionServiceImpl.java

package com.ce.pms.renovation.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.util.Assert;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.common.service.IOrganizationalStructureService;
import com.ce.pms.common.service.ISysrefcodeService;
import com.ce.pms.common.vo.OrganizationalStructureVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.service.IHouseService;
import com.ce.pms.renovation.dao.IFirstInspectionDao;
import com.ce.pms.renovation.dao.IInspectionRequisitionDao;
import com.ce.pms.renovation.dao.IInspectionViolationDao;
import com.ce.pms.renovation.dao.IReinspectionDao;
import com.ce.pms.renovation.dao.IRenovateLicenceDao;
import com.ce.pms.renovation.dao.IRenovateViolationDao;
import com.ce.pms.renovation.model.AbstractInspectionDO;
import com.ce.pms.renovation.model.FirstInspectionDO;
import com.ce.pms.renovation.model.InspectionRequisitionDO;
import com.ce.pms.renovation.model.InspectionViolationDO;
import com.ce.pms.renovation.model.ReinspectionDO;
import com.ce.pms.renovation.model.RenovateLicenceDO;
import com.ce.pms.renovation.model.RenovateViolationDO;
import com.ce.pms.renovation.service.IInspectionService;
import com.ce.pms.renovation.vo.AbstractInspectionVO;
import com.ce.pms.renovation.vo.FirstInspectionVO;
import com.ce.pms.renovation.vo.InspectionRequisitionQueryConditionVO;
import com.ce.pms.renovation.vo.InspectionRequisitionVO;
import com.ce.pms.renovation.vo.InspectionViolationVO;
import com.ce.pms.renovation.vo.ReinspectionVO;
import com.ce.util.BeanUtil;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;
import com.sitechasia.webx.core.support.Page;
import com.sitechasia.webx.core.utils.base.PopulateUtil;

/**
 * 名称: com.ce.pms.renovation.service.impl.InspectionServiceImpl
 * 验收管理服务实现类
 *
 * @author  许心炜  <br>
 * Created on： 2008-12-19 <br>
 * @author <br>
 * update on： <br>
 * why: <br>
 */
public class InspectionServiceImpl extends PmsBaseServiceImpl implements
		IInspectionService {

	private IReinspectionDao reinspectionDao;

	private IInspectionRequisitionDao inspectionRequisitionDao;

	private IRenovateLicenceDao renovateLicenceDao;

	private IFirstInspectionDao firstInspectionDao;

	private IInspectionViolationDao inspectionViolationDao;

	private IRenovateViolationDao renovateViolationDao;

	private IOrganizationalStructureService organizationalStructureService;

	private ISysrefcodeService sysrefcodeService;

	private IHouseService houseService;

	/**
	 * @roseuid 49473AFA03A9
	 */
	public InspectionServiceImpl() {

	}

	/**
	 * 查询装修验收申请记录
	 * @author 许心炜
	 * @param queryConditionVO - 组合查询条件
	 * @return Page
	 * @roseuid 49473AFB005D
	 */
	public Page queryInspectionRequisitions(
			InspectionRequisitionQueryConditionVO queryConditionVO) {
		if (queryConditionVO != null) {
			Page page = this.inspectionRequisitionDao
					.queryInspectionRequisitions(queryConditionVO);
			return page;
		} else {
			return null;
		}
	}

	/**
	 * 新增装修验收申请
	 * @author 许心炜
	 * @param inspectionRequisitionVO - 装修验收申请VO
	 * @return InspectionRequisitionVO
	 * @roseuid 49473AFB006D
	 */
	public InspectionRequisitionVO addInspectionRequisition(
			InspectionRequisitionVO inspectionRequisitionVO) {
		if (!checkInspectionRequisitionField(inspectionRequisitionVO)) {
			throw new BusinessException("common:mustFill_null_exception",
					BusinessException.WARNING);
		}

		Long licenceId = inspectionRequisitionVO.getRenovateLicenceId();
		if (BlankUtil.isBlank(licenceId)) {
			throw new BusinessException(
					"renovate:renovate_renovateviolation_nolicence_id_not_add",
					BusinessException.ERROR);
		} else {
			if (!isAllowAddInspectionRequisition(licenceId)) {
				throw new BusinessException(
						"renovate:inspection_inspectionrequisition_renovateviolation_exist_not_add",
						BusinessException.ERROR);
			}
		}

		InspectionRequisitionDO inspectionRequisitionDO = new InspectionRequisitionDO();
		inspectionRequisitionDO = assembleInspectionRequisitionV2D(
				inspectionRequisitionVO, inspectionRequisitionDO);

		inspectionRequisitionDO.setSysInfo(new SysInfo(inspectionRequisitionVO
				.getCreator(), inspectionRequisitionDao.getDate()));
		inspectionRequisitionDO.setInpectionState("");//新建
		inspectionRequisitionDO = inspectionRequisitionDao
				.saveObject(inspectionRequisitionDO);
		notifyAddInspectionRequisition(licenceId);
		inspectionRequisitionVO = assembleInspectionRequisitionD2V(
				inspectionRequisitionDO, inspectionRequisitionVO);
		return inspectionRequisitionVO;
	}

	/**
	 * 删除装修验收申请
	 * @author 许心炜
	 * @param inspectionRequisitionId - 装修验收申请ID
	 * @roseuid 49473AFB007D
	 */
	public Boolean deleteInspectionRequisition(Long inspectionRequisitionId) {
		if (!isAllowDeleteInspectionRequisition(inspectionRequisitionId)) {
			//		   throw new BusinessException("已经添加了初验或者复验了，不能删除");
			throw new BusinessException("common:delete_error_exception",
					BusinessException.FAILURE);
		}

		notifyDeleteInspectionRequisition(inspectionRequisitionId);
		try {
			inspectionRequisitionDao.deleteObjectById(inspectionRequisitionId);
		} catch (RuntimeException re) {
			throw new BusinessException(
					"inspection:inspection_inspectionrequisition_not_del_refresh",
					BusinessException.ERROR);
		}

		return true;
	}

	/**
	 * 编辑装修验收申请
	 * @author 许心炜
	 * @param inspectionRequisitionVO - 装修验收申请VO
	 * @return InspectionRequisitionVO
	 * @roseuid 49473AFB007F
	 */
	public InspectionRequisitionVO editInspectionRequisition(
			InspectionRequisitionVO inspectionRequisitionVO) {
		if (!checkInspectionRequisitionField(inspectionRequisitionVO)) {
			throw new BusinessException("common:mustFill_null_exception",
					BusinessException.WARNING);
		}

		if (!isAllowEditInspectionRequisition(inspectionRequisitionVO
				.getKeyword())) {
			throw new BusinessException(
					"inspection:inspection_inspectionrequisition_firstinspection_exist_not_edit",
					BusinessException.ERROR);
		}

		InspectionRequisitionDO inspectionRequisitionDO = inspectionRequisitionDao
				.findById(inspectionRequisitionVO.getKeyword());
		inspectionRequisitionDO = assembleInspectionRequisitionV2D(
				inspectionRequisitionVO, inspectionRequisitionDO);

		inspectionRequisitionDO.setSysInfo(new SysInfo(inspectionRequisitionVO
				.getModifier(), inspectionRequisitionDao.getDate()));
		inspectionRequisitionDO = inspectionRequisitionDao
				.updateObject(inspectionRequisitionDO);

		inspectionRequisitionVO = assembleInspectionRequisitionD2V(
				inspectionRequisitionDO, inspectionRequisitionVO);
		return inspectionRequisitionVO;
	}

	/**
	 * 根据ID获得装修验收申请详细信息
	 * @author 许心炜
	 * @param inspectionRequisitionId - 装修验收申请ID
	 * @return InspectionRequisitionVO
	 * @roseuid 49473AFB009C
	 */
	public InspectionRequisitionVO getInspectionRequisition(
			Long inspectionRequisitionId) {
		if (BlankUtil.isBlank(inspectionRequisitionId)) {
			throw new BusinessException("请先选择一条记录");
		}

		InspectionRequisitionVO inspectionRequisitionVO = new InspectionRequisitionVO();
		InspectionRequisitionDO inspectionRequisitionDO = inspectionRequisitionDao
				.getInspectionRequisition(inspectionRequisitionId);

		if (inspectionRequisitionDO != null) {
			inspectionRequisitionVO = assembleInspectionRequisitionD2V(
					inspectionRequisitionDO, inspectionRequisitionVO);
			return inspectionRequisitionVO;
		} else {
			return null;
		}

	}

	/** 功能描述: 根据许可证ID获得装修验收对象 <br>
	 * @param licenceId - 许可证ID
	 * @return
	 * Create author:许心炜 <br>
	 * Create on:2008-12-29  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public InspectionRequisitionVO getInspectionRequisitionByLicenceId(
			Long licenceId) {
		InspectionRequisitionVO inspectionRequisitionVO = new InspectionRequisitionVO();
		InspectionRequisitionDO inspectionRequisitionDO = inspectionRequisitionDao
				.getInspectionRequisitionByLicenceId(licenceId);

		if (inspectionRequisitionDO != null) {
			inspectionRequisitionVO = assembleInspectionRequisitionD2V(
					inspectionRequisitionDO, inspectionRequisitionVO);
			return inspectionRequisitionVO;
		} else {
			return null;
		}
	}

	/**
	 * 竣工初验
	 * @author 许心炜
	 * @param firstInspectionVO - 竣工初验VO
	 * @roseuid 49473AFB00AB
	 */
	public FirstInspectionVO firstInspection(FirstInspectionVO firstInspectionVO) {
		if (!isAllowFirstInspection(firstInspectionVO
				.getInspectionRequisitionId())) {
			throw new BusinessException(
					"renovate:inspection_firstinspection_has_reinspection_not_add_or_edit",
					BusinessException.ERROR);
		}

		FirstInspectionDO firstInspectionDO = firstInspectionDao
				.getFirstInspection(firstInspectionVO
						.getInspectionRequisitionId());//getid
		//如果没有初验记录则新增
		if (firstInspectionDO == null) {
			firstInspectionVO = addFirstInspection(firstInspectionVO);
		}

		//如果有初验记录则进行修改操作
		if (firstInspectionDO != null) {
			firstInspectionVO = editFirstInspection(firstInspectionVO);
		}

		return firstInspectionVO;
	}

	/**
	 * 根据装修验收申请的ID获得竣工初验的详细信息
	 * @author 许心炜
	 * @param inspectionRequisitionId - 装修验收申请ID
	 * @return FirstInspectionVO
	 * @roseuid 49473AFB00AD
	 */
	public FirstInspectionVO getFirstInspection(Long inspectionRequisitionId) {
		if (BlankUtil.isBlank(inspectionRequisitionId)) {
			throw new BusinessException("请先选择一条记录");
		}

		FirstInspectionVO firstInspectionVO = new FirstInspectionVO();
		FirstInspectionDO firstInspectionDO = firstInspectionDao
				.getFirstInspection(inspectionRequisitionId);
		if (firstInspectionDO != null) {
			firstInspectionVO = (FirstInspectionVO) assembleInspectionD2V(
					firstInspectionDO, firstInspectionVO);
			return firstInspectionVO;
		} else {
			return null;
		}

	}

	/**
	 * 根据抽象装修验收ID获得验收期违规记录
	 * @author 许心炜
	 * @param inspectionId - 抽象装修验收ID,复验id
	 * @return InspectionViolationVO
	 * @roseuid 49473AFB00BC
	 */
	public InspectionViolationVO getInspectionViolation(Long inspectionId) {
		if (BlankUtil.isBlank(inspectionId)) {
			throw new BusinessException("请先选择一条记录");
		}

		InspectionViolationVO inspectionViolationVO = new InspectionViolationVO();
		InspectionViolationDO inspectionViolationDO = inspectionViolationDao
				.getInspectionViolation(inspectionId);

		if (inspectionViolationDO != null) {
			inspectionViolationVO = assembleInspectionViolationD2V(
					inspectionViolationDO, inspectionViolationVO);
			return inspectionViolationVO;
		} else {
			return null;
		}

	}

	/**
	 * 验收期违规登记：在初验时，不用对违规登记验证了
	 * @author 许心炜
	 * @param inspectionViolationVO - 装修期违规记录VO
	 * @return InspectionViolationVO
	 * @roseuid 49473AFB00CC
	 */
	public InspectionViolationVO registerInspectionViolation(
			InspectionViolationVO inspectionViolationVO) {
		//	   if(!isAllowRegisterInspectionViolation(inspectionViolationVO.getFirstInspectionId())){
		//		  	throw new BusinessException("不能在修改了");
		//	   }

		if (inspectionViolationVO.getFirstInspectionId() != null) {//初验时的违规登记
			List<ReinspectionDO> list = reinspectionDao
					.getReinspections(inspectionViolationVO
							.getInspectionRequisitionId());//需要验收申请ID//findById(inspectionViolationVO.getFirstInspectionId());

			if (list.size() > 0) {//如果有复验记录，初验时不可以修改和新增
				throw new BusinessException(
						"renovate:inspection_firstinspection_has_reinspection_not_add_or_edit",
						BusinessException.ERROR);
			} else {//如果还没有复验记录，初验时可以修改和新增
				InspectionViolationDO inspectionViolationDO = inspectionViolationDao
						.getInspectionViolation(inspectionViolationVO
								.getFirstInspectionId());

				//DO==null时为新增，否则为修改
				if (inspectionViolationDO == null) {
					inspectionViolationVO = addInspectionViolation(inspectionViolationVO);
				}

				if (inspectionViolationDO != null) {
					inspectionViolationVO = editInspectionViolation(inspectionViolationVO);
				}
			}
		}

		if (inspectionViolationVO.getReinspectionId() != null) {//复验时的违规登记
			if (!isAllowRegisterInspectionViolation(inspectionViolationVO
					.getReinspectionId())) {
				throw new BusinessException("不能在修改了");
			}

			InspectionViolationDO inspectionViolationDO = inspectionViolationDao
					.getInspectionViolation(inspectionViolationVO
							.getReinspectionId());

			//DO==null时为新增，否则为修改
			if (inspectionViolationDO == null) {
				inspectionViolationVO = addInspectionViolation(inspectionViolationVO);
			}

			if (inspectionViolationDO != null) {
				inspectionViolationVO = editInspectionViolation(inspectionViolationVO);
			}
		}

		if (inspectionViolationVO != null) {
			return inspectionViolationVO;
		} else {
			return null;
		}
	}

	/**
	 * 根据验收申请ID获得竣工复验记录列表：前台初验时，如果存在有复验记录时候做的验证的判断
	 * @author 许心炜
	 * @param inspectionRequisitionId - 验收申请ID
	 * @return List
	 * @roseuid 49473AFB00DB
	 */
	public List<ReinspectionVO> getReinspections(Long inspectionRequisitionId) {
		List<ReinspectionDO> list = reinspectionDao
				.getReinspections(inspectionRequisitionId);
		List<ReinspectionVO> list2 = new ArrayList<ReinspectionVO>();

		for (ReinspectionDO reinspectionDO : list) {
			ReinspectionVO reinspectionVO = new ReinspectionVO();
			assembleInspectionD2V(reinspectionDO, reinspectionVO);
			list2.add(reinspectionVO);
		}

		return list2;
	}

	/**
	 * 新增竣工复验
	 * @author 许心炜
	 * @param reinspection - 竣工复验VO
	 * @return ReinspectionVO
	 * @roseuid 49473AFB00EB
	 */
	public ReinspectionVO addReinspection(ReinspectionVO reinspection) {
		if (!BlankUtil.isBlank(reinspection.getInspectionDateStr())) {
			reinspection.setInspectionDate(DateUtil.convertStrToDate(
					reinspection.getInspectionDateStr(), "yyyy-MM-dd HH时"));
		} else {
			throw new BusinessException("common:mustFill_null_exception",
					BusinessException.WARNING);
		}

		if (!checkReinspectionField(reinspection)) {
			throw new BusinessException("common:mustFill_null_exception",
					BusinessException.WARNING);
		}

		if (!isAllowAddReinspection(reinspection.getInspectionRequisitionId())) {
			throw new BusinessException(
					"renovate:inspection_reinspection_no_firstinspection_not_add",
					BusinessException.ERROR);
		}
		ReinspectionDO reinspectionDO = new ReinspectionDO();
		reinspectionDO = (ReinspectionDO) assembleInspectionV2D(reinspection,
				reinspectionDO);
		reinspectionDO.setSysInfo(new SysInfo(reinspection.getCreator(),
				this.reinspectionDao.getDate()));
		InspectionRequisitionDO inspectionRequisitionDO = inspectionRequisitionDao
				.findById(reinspection.getInspectionRequisitionId());
		reinspectionDO.setInspectionRequisition(inspectionRequisitionDO);
		reinspectionDO = reinspectionDao.saveObject(reinspectionDO);
		notifyAddOrEditReinspection(reinspectionDO.getKeyword());
		reinspection = (ReinspectionVO) assembleInspectionD2V(reinspectionDO,
				reinspection);
		return reinspection;
	}

	/**
	 * 修改竣工复验:
	 * @author 许心炜
	 * @param reinspection - 竣工复验VO
	 * @return ReinspectionVO
	 * @roseuid 49473AFB0109
	 */
	public ReinspectionVO editReinspection(ReinspectionVO reinspection) {
		if (!checkReinspectionField(reinspection)) {
			throw new BusinessException("common:mustFill_null_exception",
					BusinessException.WARNING);
		}

		if (!isAllowEditReinspection(reinspection.getKeyword())) {
			throw new BusinessException(
					"renovate:inspection_reinspection_has_passed_not_add",
					BusinessException.ERROR);
		}

		ReinspectionDO reinspectionDO = reinspectionDao.findById(reinspection
				.getKeyword());

		reinspectionDO = (ReinspectionDO) assembleInspectionV2D(reinspection,
				reinspectionDO);
		reinspectionDO.setSysInfo(new SysInfo(reinspection.getModifier(),
				this.reinspectionDao.getDate()));

		reinspectionDO = reinspectionDao.updateObject(reinspectionDO);
		reinspection = (ReinspectionVO) assembleInspectionD2V(reinspectionDO,
				reinspection);
		notifyAddOrEditReinspection(reinspection.getKeyword());
		return reinspection;
	}

	/**
	 * 删除竣工复验
	 * @author 许心炜
	 * @param reinspectionId - 竣工复验ID
	 * @roseuid 49473AFB0119
	 */
	public Boolean deleteReinspection(Long reinspectionId) {
		if (!isAllowDeleteReinspection(reinspectionId)) {
			throw new BusinessException(
					"renovate:inspection_reinspection_has_passed_not_del",
					BusinessException.ERROR);
		}

		notifyDeleteReinspection(reinspectionId);
		reinspectionDao.deleteObjectById(reinspectionId);
		return true;
	}

	/**
	 * 根据ID获得竣工复验VO
	 * @author 许心炜
	 * @param reinspectionId - 竣工复验ID
	 * @return ReinspectionVO
	 * @roseuid 49473AFB011B
	 */
	public ReinspectionVO getReinspection(Long reinspectionId) {
		if (reinspectionId == null) {
			throw new BusinessException("复验id不能为空");
		}

		ReinspectionVO reinspectionVO = new ReinspectionVO();
		ReinspectionDO reinspectionDO = reinspectionDao
				.findById(reinspectionId);

		if (reinspectionDO != null) {
			//根据竣工复验获取验收申请ID
			InspectionRequisitionDO inspectionRequisitionDO = reinspectionDO
					.getInspectionRequisition();

			reinspectionVO = (ReinspectionVO) assembleInspectionD2V(
					reinspectionDO, reinspectionVO);

			//验收状态，001-新建， 002-竣工初验， 003-复验不通过， 004-复验通过
			if ("004".equals(inspectionRequisitionDO.getInpectionState())) {
				// 如果复验通过，设置一个字段通知前台“不通过”状态的违规就不能改了，并且复验记录也不能修改,002状态代表已经做过保证金退还了
				reinspectionVO.setState("004");
			} else if ("003"
					.equals(inspectionRequisitionDO.getInpectionState())) {
				reinspectionVO.setState("003");
			}

			if ("004".equals(inspectionRequisitionDO.getRenovateLicence()
					.getRenovateRequisition().getProcessState())) {
				reinspectionVO.setState("002");
			}
		}

		if (reinspectionVO != null) {
			return reinspectionVO;
		} else {
			return null;
		}

	}

	/** 功能描述: 根据许可证号获得通过的复验记录 <br>
	 * @param licenceId
	 * @return
	 * Create author:许心炜 <br>
	 * Create on:2009-1-16  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public ReinspectionVO getPassReinspectionByLicenceId(Long licenceId) {
		if (licenceId == null) {
			return null;
		} else {
			ReinspectionDO reinspectionDO = reinspectionDao
					.getPassReinspectionByLicenceId(licenceId);
			if (reinspectionDO != null) {
				ReinspectionVO reinspectionVO = new ReinspectionVO();
				reinspectionVO = (ReinspectionVO) assembleInspectionD2V(
						reinspectionDO, reinspectionVO);
				return reinspectionVO;
			} else {
				return null;
			}
		}
	}

	/**
	 * 新增竣工初验
	 * @author 许心炜
	 * @param firstInspectionVO
	 * @roseuid 493F82C60271
	 */
	private FirstInspectionVO addFirstInspection(
			FirstInspectionVO firstInspectionVO) {
		FirstInspectionDO tempFirstInspectionDO = new FirstInspectionDO();
		tempFirstInspectionDO = (FirstInspectionDO) assembleInspectionV2D(
				firstInspectionVO, tempFirstInspectionDO);
		tempFirstInspectionDO.setSysInfo(new SysInfo(firstInspectionVO
				.getCreator(), firstInspectionDao.getDate()));
		InspectionRequisitionDO inspectionRequisitionDO = inspectionRequisitionDao
				.getInspectionRequisition(firstInspectionVO
						.getInspectionRequisitionId());
		tempFirstInspectionDO.setInspectionRequisition(inspectionRequisitionDO);
		tempFirstInspectionDO = firstInspectionDao
				.saveObject(tempFirstInspectionDO);
		if (tempFirstInspectionDO != null) {
			firstInspectionVO = (FirstInspectionVO) assembleInspectionD2V(
					tempFirstInspectionDO, firstInspectionVO);
			notifyFirstInspection(firstInspectionVO
					.getInspectionRequisitionId());//getid
			return firstInspectionVO;
		} else {
			return null;
		}

	}

	/**
	 * 修改竣工初验
	 * @author 许心炜
	 * @param firstInspectionVO
	 * @roseuid 493F832402AF
	 */
	private FirstInspectionVO editFirstInspection(
			FirstInspectionVO firstInspectionVO) {
		FirstInspectionDO firstInspectionDO = firstInspectionDao
				.getFirstInspection(firstInspectionVO
						.getInspectionRequisitionId());

		firstInspectionDO = (FirstInspectionDO) assembleInspectionV2D(
				firstInspectionVO, firstInspectionDO);
		firstInspectionDO.setSysInfo(new SysInfo(firstInspectionVO
				.getModifier(), reinspectionDao.getDate()));

		firstInspectionDO = firstInspectionDao.updateObject(firstInspectionDO);

		if (firstInspectionDO != null) {
			firstInspectionVO = (FirstInspectionVO) assembleInspectionD2V(
					firstInspectionDO, firstInspectionVO);
			notifyFirstInspection(firstInspectionVO
					.getInspectionRequisitionId());
			return firstInspectionVO;
		} else {
			return null;
		}

	}

	/**
	 * 新增验收期违规：存在一条竣工申请记录
	 * @author 许心炜
	 * @param inspectionViolationVO
	 * @roseuid 493F894D008C
	 */
	private InspectionViolationVO addInspectionViolation(
			InspectionViolationVO inspectionViolationVO) {
		if (!checkRenovateViolationField(inspectionViolationVO)) {
			throw new BusinessException("common:mustFill_null_exception",
					BusinessException.WARNING);
		}

		InspectionViolationDO tempInspectionViolationDO = new InspectionViolationDO();
		tempInspectionViolationDO = assembleInspectionViolationV2D(
				inspectionViolationVO, tempInspectionViolationDO);
		tempInspectionViolationDO.setSysInfo(new SysInfo(inspectionViolationVO
				.getCreator(), inspectionRequisitionDao.getDate()));

		//	   RenovateLicenceDO renovateLicenceDO = renovateLicenceDao.findById(inspectionViolationVO.getRenovateLicenceId());
		if (inspectionViolationVO.getFirstInspectionId() != null) {//初验时新增
			AbstractInspectionDO abstractInspectionDO = firstInspectionDao
					.findById(inspectionViolationVO.getFirstInspectionId());
			FirstInspectionDO firstInspectionDO = firstInspectionDao
					.findById(inspectionViolationVO.getFirstInspectionId());
			tempInspectionViolationDO.setRenovateLicence(firstInspectionDO
					.getInspectionRequisition().getRenovateLicence());
			tempInspectionViolationDO.setInspection(abstractInspectionDO);

		}

		if (inspectionViolationVO.getReinspectionId() != null) {//复验时新增
			AbstractInspectionDO abstractInspectionDO = reinspectionDao
					.findById(inspectionViolationVO.getReinspectionId());
			ReinspectionDO reinspectionDO = reinspectionDao
					.findById(inspectionViolationVO.getReinspectionId());
			tempInspectionViolationDO.setRenovateLicence(reinspectionDO
					.getInspectionRequisition().getRenovateLicence());
			tempInspectionViolationDO.setInspection(abstractInspectionDO);
		}

		tempInspectionViolationDO = (InspectionViolationDO) inspectionViolationDao
				.saveObject(tempInspectionViolationDO);
		if (tempInspectionViolationDO != null) {
			inspectionViolationVO = assembleInspectionViolationD2V(
					tempInspectionViolationDO, inspectionViolationVO);
			return inspectionViolationVO;
		} else {
			return null;
		}
	}

	/**
	 * 修改验收期违规
	 * @author 许心炜
	 * @param inspectionViolationVO
	 * @roseuid 493F898E0157
	 */
	private InspectionViolationVO editInspectionViolation(
			InspectionViolationVO inspectionViolationVO) {
		if (!checkRenovateViolationField(inspectionViolationVO)) {
			throw new BusinessException("common:mustFill_null_exception",
					BusinessException.WARNING);
		}

		InspectionViolationDO tempInspectionViolationDO = new InspectionViolationDO();

		if (inspectionViolationVO.getFirstInspectionId() != null) {//初验时新增
			tempInspectionViolationDO = inspectionViolationDao
					.getInspectionViolation(inspectionViolationVO
							.getFirstInspectionId());
			AbstractInspectionDO abstractInspectionDO = firstInspectionDao
					.findById(inspectionViolationVO.getFirstInspectionId());
			FirstInspectionDO firstInspectionDO = firstInspectionDao
					.findById(inspectionViolationVO.getFirstInspectionId());
			tempInspectionViolationDO.setRenovateLicence(firstInspectionDO
					.getInspectionRequisition().getRenovateLicence());
			tempInspectionViolationDO.setInspection(abstractInspectionDO);
		}

		if (inspectionViolationVO.getReinspectionId() != null) {//复验时新增
			tempInspectionViolationDO = inspectionViolationDao
					.getInspectionViolation(inspectionViolationVO
							.getReinspectionId());
			AbstractInspectionDO abstractInspectionDO = reinspectionDao
					.findById(inspectionViolationVO.getReinspectionId());
			ReinspectionDO reinspectionDO = reinspectionDao
					.findById(inspectionViolationVO.getReinspectionId());
			tempInspectionViolationDO.setRenovateLicence(reinspectionDO
					.getInspectionRequisition().getRenovateLicence());
			tempInspectionViolationDO.setInspection(abstractInspectionDO);
		}

		tempInspectionViolationDO = assembleInspectionViolationV2D(
				inspectionViolationVO, tempInspectionViolationDO);
		tempInspectionViolationDO.setSysInfo(new SysInfo(inspectionViolationVO
				.getModifier(), inspectionRequisitionDao.getDate()));
		tempInspectionViolationDO = (InspectionViolationDO) inspectionViolationDao
				.updateObject(tempInspectionViolationDO);

		if (tempInspectionViolationDO != null) {
			inspectionViolationVO = assembleInspectionViolationD2V(
					tempInspectionViolationDO, inspectionViolationVO);
			return inspectionViolationVO;
		} else {
			return null;
		}
	}

	/**
	 * @author 许心炜
	 * @param inspectionRequisitionVO
	 * @param inspectionRequisitionDO
	 * @return com.ce.pms.renovation.model.InspectionRequisitionDO
	 * @roseuid 493F5BB0033C
	 */
	private InspectionRequisitionDO assembleInspectionRequisitionV2D(
			InspectionRequisitionVO inspectionRequisitionVO,
			InspectionRequisitionDO inspectionRequisitionDO) {
		BeanUtil
				.convertVoAsDo(inspectionRequisitionVO, inspectionRequisitionDO);

		inspectionRequisitionDO.setInspecteDate(inspectionRequisitionVO
				.getInspecteDate());
		if (!BlankUtil.isBlank(inspectionRequisitionVO.getRenovateLicenceId())) {
			RenovateLicenceDO renovateLicenceDO = renovateLicenceDao
					.findById(inspectionRequisitionVO.getRenovateLicenceId());
			renovateLicenceDO.getRenovateRequisition().setBurdenManPhone(
					inspectionRequisitionVO.getBurdenManPhone());
			renovateLicenceDO.getRenovateRequisition().setLinkManPhone(
					inspectionRequisitionVO.getLinkManPhone());
			inspectionRequisitionDO.setRenovateLicence(renovateLicenceDO);
		}
		return inspectionRequisitionDO;
	}

	/**
	 * @author 许心炜
	 * @param inspectionRequisitionDO
	 * @param inspectionRequisitionVO
	 * @return InspectionRequisitionVO
	 * @roseuid 493F5CB00138
	 */
	private InspectionRequisitionVO assembleInspectionRequisitionD2V(
			InspectionRequisitionDO inspectionRequisitionDO,
			InspectionRequisitionVO inspectionRequisitionVO) {
		BeanUtil
				.convertVoAsDo(inspectionRequisitionDO, inspectionRequisitionVO);

		inspectionRequisitionVO.setInpectionState(inspectionRequisitionDO
				.getInpectionState());
		inspectionRequisitionVO.setHouseNumber(inspectionRequisitionDO
				.getRenovateLicence().getRenovateRequisition().getHouse()
				.getHouseNumber());//房号
		inspectionRequisitionVO.setBurdenMan(inspectionRequisitionDO
				.getRenovateLicence().getRenovateRequisition().getBurdenMan());//负责人
		inspectionRequisitionVO.setRenovateLicenceId(inspectionRequisitionDO
				.getRenovateLicence().getKeyword());//许可证id
		inspectionRequisitionVO.setRenovateLicence(inspectionRequisitionDO
				.getRenovateLicence().getLicenceNumber());//许可证
		inspectionRequisitionVO
				.setClientNames(inspectionRequisitionDO.getRenovateLicence()
						.getRenovateRequisition().getClientNames());//业主
		inspectionRequisitionVO.setLinkMan(inspectionRequisitionDO
				.getRenovateLicence().getRenovateRequisition().getLinkman());//联系人
		inspectionRequisitionVO
				.setCompanyName(inspectionRequisitionDO.getRenovateLicence()
						.getRenovateRequisition().getCompanyName());//装修承建商名字
		inspectionRequisitionVO.setCreator(inspectionRequisitionDO.getSysInfo()
				.getCreator());//接单人ID

		OrganizationalStructureVO organizationalStructureVO = organizationalStructureService
				.queryUserByUserId(inspectionRequisitionDO.getSysInfo()
						.getCreator());
		inspectionRequisitionVO.setAnnalMan(organizationalStructureVO
				.getUserCode());//接单人

		inspectionRequisitionVO.setCreateDateStr(getDateString("yyyy-MM-dd",
				inspectionRequisitionDO.getSysInfo().getSysDate()));//接单时间
		inspectionRequisitionVO.setFinishDateStr(getDateString("yyyy-MM-dd",
				inspectionRequisitionDO.getFinishDate()));//竣工时间
		inspectionRequisitionVO.setInspecteDateStr(DateUtil.convertDateToStr(
				inspectionRequisitionDO.getInspecteDate(), "yyyy-MM-dd HH时"));//申请验收时间，精确到小时

		return inspectionRequisitionVO;
	}

	/**
	 * @author 许心炜
	 * @param abstractInspectionVO
	 * @param abstractInspectionDO
	 * @return com.ce.pms.renovation.model.AbstractInspectionDO
	 * @roseuid 493F833602CE
	 */
	private AbstractInspectionDO assembleInspectionV2D(
			AbstractInspectionVO abstractInspectionVO,
			AbstractInspectionDO abstractInspectionDO) {
		BeanUtil.convertVoAsDo(abstractInspectionVO, abstractInspectionDO);

		return abstractInspectionDO;
	}

	/**
	 * @author 许心炜
	 * @param abstractInspectionDO
	 * @param abstractInspectionVO
	 * @return AbstractInspectionVO
	 * @roseuid 493F83610186
	 */
	private AbstractInspectionVO assembleInspectionD2V(
			AbstractInspectionDO abstractInspectionDO,
			AbstractInspectionVO abstractInspectionVO) {
		BeanUtil.convertVoAsDo(abstractInspectionDO, abstractInspectionVO);
		abstractInspectionVO.setInspectionDateStr(DateUtil.convertDateToStr(
				abstractInspectionDO.getInspectionDate(), "yyyy-MM-dd HH时"));
		return abstractInspectionVO;
	}

	/**
	 * @author 许心炜
	 * @param inspectionViolationVO
	 * @param inspectionViolationDO
	 * @return com.ce.pms.renovation.model.InspectionViolationDO
	 * @roseuid 493F8803009C
	 */
	private InspectionViolationDO assembleInspectionViolationV2D(
			InspectionViolationVO inspectionViolationVO,
			InspectionViolationDO inspectionViolationDO) {
//		PopulateUtil.populate(inspectionViolationVO, inspectionViolationDO);
		BeanUtil.convertVoAsDo(inspectionViolationVO, inspectionViolationDO);

		if(BlankUtil.isBlank(inspectionViolationVO.getReformDate())) {
			inspectionViolationDO.setReformDate(null);
		}

		if(BlankUtil.isBlank(inspectionViolationVO.getAmerceClient())) {
			inspectionViolationDO.setAmerceClient(new BigDecimal(0));
		}

		if(BlankUtil.isBlank(inspectionViolationVO.getAmerceCompany())) {
			inspectionViolationDO.setAmerceCompany(new BigDecimal(0));
		}

		if(BlankUtil.isBlank(inspectionViolationVO.getSeizureRes())) {
			inspectionViolationDO.setSeizureRes("");
		}

		if(BlankUtil.isBlank(inspectionViolationVO.getOtherRemark())) {
			inspectionViolationDO.setOtherRemark("");
		}


		if (!BlankUtil.isBlank(inspectionViolationVO.getReinspectionId())) {
			ReinspectionDO reinspectionDO = reinspectionDao
					.findById(inspectionViolationVO.getReinspectionId());
			inspectionViolationDO.setInspection(reinspectionDO);
		}

		if (!BlankUtil.isBlank(inspectionViolationVO
				.getInspectionRequisitionId())) {
			InspectionRequisitionDO inspectionRequisitionDO = inspectionRequisitionDao
					.findById(inspectionViolationVO
							.getInspectionRequisitionId());
			RenovateLicenceDO renovateLicenceDO = inspectionRequisitionDO
					.getRenovateLicence();
			renovateLicenceDO.getRenovateRequisition().setBurdenManPhone(
					inspectionViolationVO.getBurdenManPhone());
			renovateLicenceDO.getRenovateRequisition().setLinkManPhone(
					inspectionViolationVO.getLinkManPhone());
			inspectionViolationDO.setRenovateLicence(renovateLicenceDO);
		}

		return inspectionViolationDO;
	}

	/**
	 * 功能描述:  <br>
	 * @param inspectionViolationDO
	 * @param inspectionViolationVO
	 * @return InspectionViolationVO
	 * Create author:许心炜 <br>
	 * Create on:2008-12-23  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private InspectionViolationVO assembleInspectionViolationD2V(
			InspectionViolationDO inspectionViolationDO,
			InspectionViolationVO inspectionViolationVO) {
		BeanUtil.convertVoAsDo(inspectionViolationDO, inspectionViolationVO);
		inspectionViolationVO.setHouseNumber(inspectionViolationDO
				.getRenovateLicence().getRenovateRequisition().getHouse()
				.getHouseNumber());//房号
		inspectionViolationVO
				.setOwner(inspectionViolationDO.getRenovateLicence()
						.getRenovateRequisition().getClientNames());//业主
		inspectionViolationVO.setRenovateLicenceNO(inspectionViolationDO
				.getRenovateLicence().getLicenceNumber());//许可证
		inspectionViolationVO.setLinkMan(inspectionViolationDO
				.getRenovateLicence().getRenovateRequisition().getLinkman());//联系人
		inspectionViolationVO.setBurdenMan(inspectionViolationDO
				.getRenovateLicence().getRenovateRequisition().getBurdenMan());//现场负责人电话
		return inspectionViolationVO;
	}

	/**
	 * 是否允许新增装修验收申请:许可证已发，并且不存在装修违规（出于未整改和通过状态就说明现在没有违规的存在）
	 * @author 许心炜
	 * @param licenceId - 许可证ID
	 * @return Boolean
	 * @roseuid 493F638D02AF
	 */
	private Boolean isAllowAddInspectionRequisition(Long licenceId) {
		//根据ID查到违规记录
		List<RenovateViolationDO> list = renovateViolationDao
				.getRenovateViolationByLicenceID(licenceId);
		//根据ID查到许可证
		RenovateLicenceDO renovateLicenceDO = this.renovateLicenceDao
				.findById(licenceId);
		//根据许可证ID查此id的验收申请
		InspectionRequisitionDO inspectionRequisitionDO = inspectionRequisitionDao
				.getInspectionRequisitionByLicenceId(licenceId);

		//发证状态 001-已发，002-未发;整改状态 001-未整改， 002-不通过， 003-通过
		if (inspectionRequisitionDO == null) {//没有验收申请时
			if (list.size() == 0) {//没有违规记录
				if (("001").equals(renovateLicenceDO.getState())) {//许可证已发
					return true;
				} else {//许可证未发
					throw new BusinessException(
							"renovate:renovate_renovateviolation_nolicence_id_not_add",
							BusinessException.ERROR);
					//				   return false;
				}
			} else {//有违规记录
				throw new BusinessException(
						"renovate:inspection_inspectionrequisition_renovateviolation_exist_not_passed_not_add",
						BusinessException.ERROR);
				//			   return false;
			}
		} else {//有验收申请时
			throw new BusinessException(
					"renovate:renovate_renovateviolation_has_inspectionrequisitions_not_add",
					BusinessException.ERROR);
			//		   return false;
		}
	}

	/**
	 * 是否允许修改装修验收申请:对于已经做过初验的装修业务，其验收申请不能修改
	 * @author 许心炜
	 * @param InspectionRequisitionId - 装修验收申请ID
	 * @return boolean
	 * @roseuid 493F75F4029F
	 */
	private boolean isAllowEditInspectionRequisition(
			Long InspectionRequisitionId) {
		if (BlankUtil.isBlank(InspectionRequisitionId)) {
			throw new BusinessException("验收申请ID为空");
			//		   return false;
		}

		FirstInspectionDO firstInspectionDO = firstInspectionDao
				.getFirstInspection(InspectionRequisitionId);
		if (firstInspectionDO != null) {//已经有初验了
			throw new BusinessException(
					"renovate:inspection_inspectionrequisition_firstinspection_exist_not_edit",
					BusinessException.ERROR);
			//		   return false;
		}

		return true;
	}

	/**
	 * 是否允许删除验收申请:对于已经做过初次验收的申请记录，不能删除
	 * @author 许心炜
	 * @param inspectionRequisitionId - 验收申请ID
	 * @return boolean
	 * @roseuid 493F7AC5036B
	 */
	private boolean isAllowDeleteInspectionRequisition(
			Long inspectionRequisitionId) {
		//验收申请ID为空直接返回
//		if (BlankUtil.isBlank(inspectionRequisitionId)) {
//			throw new BusinessException("验收申请ID为空");
//			//		   return false;
//		}
		Assert.notNull(inspectionRequisitionId);

		FirstInspectionDO firstInspectionDO = firstInspectionDao
				.getFirstInspection(inspectionRequisitionId);
		if (firstInspectionDO != null) {//存在初验记录
		//		   throw new BusinessException("已经做了初验，不能删除");
			throw new BusinessException("common:delete_error_exception",
					BusinessException.FAILURE);
			//		   return false;
		}

		return true;
	}

	/**
	 * 是否允许竣工初验：初验登记对于本此装修只操作一次,已经做过了复验了就不能做初验登记了
	 * @author 许心炜
	 * @param inspectionRequisitionId - 验收申请Id
	 * @return boolean
	 * @roseuid 493F805102AF
	 */
	private boolean isAllowFirstInspection(Long inspectionRequisitionId) {
		//验收申请ID为空直接返回
		if (BlankUtil.isBlank(inspectionRequisitionId)) {
			throw new BusinessException("验收申请ID为空");
			//		   return false;
		}

		//	   FirstInspectionDO firstInspectionDO = firstInspectionDao.getFirstInspection(inspectionRequisitionId);
		//存在初验记录并且有复验的记录，则不能新增初验
		if (getReinspections(inspectionRequisitionId).size() > 0) {
			throw new BusinessException(
					"renovate:inspection_firstinspection_has_reinspection_not_add_or_edit",
					BusinessException.ERROR);
			//		   throw new BusinessException("已经做了复验，不能新增或者修改初验了");
			//		   return false;
		}

		return true;
	}

	/**
	 * 是否允许新增竣工复验：必须要做过初验，有通过的状态也不能新增
	 * @author 许心炜
	 * @param inspectionRequisitionId - 验收申请ID
	 * @return boolean
	 * @roseuid 493F8C40009C
	 */
	private boolean isAllowAddReinspection(Long inspectionRequisitionId) {
		if (BlankUtil.isBlank(inspectionRequisitionId)) {
			return false;
		}

		InspectionRequisitionDO inspectionRequisitionDO = inspectionRequisitionDao
				.findById(inspectionRequisitionId);
		//验收状态，001-新建， 002-竣工初验， 003-复验不通过， 004-复验通过
		//处于“竣工初验”或者“复验不通过”状态才能进行新增操作
		//	   if("".equals(inspectionRequisitionDO.getInpectionState()) || "003".equals(inspectionRequisitionDO.getInpectionState())){
		//		   return true;
		//	   }else{
		//		   return false;
		//	   }
		//根据装修验收申请id获取有没有初验结果
		FirstInspectionDO firstInspectionDO = firstInspectionDao
				.getFirstInspection(inspectionRequisitionId);

		if (firstInspectionDO != null) {//如果已经做了初验
			if ("004".equals(inspectionRequisitionDO.getInpectionState())) {//已经存在复验通过的记录
				throw new BusinessException(
						"renovate:inspection_reinspection_has_passed_not_add",
						BusinessException.ERROR);
			} else {//因为已经做过初验了，所以在“”和不通过状态下都可以做复验新增
				return true;
			}
		} else {
			throw new BusinessException(
					"renovate:inspection_reinspection_no_firstinspection_not_add",
					BusinessException.ERROR);
		}
	}

	/**
	 * 是否允许修改竣工复验：只能改通过的那个复验记录。如果已经通过，则不能进入修改界面.如果已经有退还保证金记录了通过的也不能改了
	 * @author 许心炜
	 * @param reinspectionId - 复验记录ID
	 * @return boolean
	 * @roseuid 493F8CB001E4
	 */
	private boolean isAllowEditReinspection(Long reinspectionId) {
		if (BlankUtil.isBlank(reinspectionId)) {
			throw new BusinessException("复验记录id为空");
			//		   return false;
		}

		//	 和是否允许登记验收期违规的验证一样
		if (isAllowRegisterInspectionViolation(reinspectionId)) {
			return true;
		} else {
			//		   throw new BusinessException("只能修改通过状态的记录");
			throw new BusinessException(
					"renovate:inspection_reinspection_just_edit_passed",
					BusinessException.ERROR);
			//		   return false;
		}
	}

	/**
	 * 是否允许删除竣工复验：必须先删除通过的，才能删除不通过的。如果已经有保证金退还的记录了，通过状态的记录也不能删除了。
	 * @author 许心炜
	 * @param reinspectionId - 验收申请ID
	 * @return boolean
	 * @roseuid 493F8D0B036B
	 */
	private boolean isAllowDeleteReinspection(Long reinspectionId) {
		if (BlankUtil.isBlank(reinspectionId)) {
			throw new BusinessException("验收申请ID为空");
			//		   return false;
		}

		ReinspectionDO reinspectionDO = reinspectionDao
				.findById(reinspectionId);
		//根据复验查询出这个验收申请是否为“复验通过”状态。
		//验收状态，001-新建， 002-竣工初验， 003-复验不通过， 004-复验通过
		if ("003".equals(reinspectionDO.getInspectionRequisition()
				.getRenovateLicence().getRenovateRequisition()
				.getProcessState())) {//处于验收期
			if ("004".equals(reinspectionDO.getInspectionRequisition()
					.getInpectionState())) {
				if ("001".equals(reinspectionDO.getResult())) {//如果删除的这个是"验收通过"的，则能删除；验收结果 001-验收通过，002-验收不通过
					return true;
				} else {//如果删除的这个是"验收不通过"的，则不能删除
					throw new BusinessException(
							"renovate:inspection_reinspection_has_passed_not_del",
							BusinessException.ERROR);
					//				   return false;
				}
			} else {//如果这个验收申请是否为“复验不通过”状态,则能够删除
				return true;
			}
		} else {
			//		   throw new BusinessException("已经做过保证金退还，不能删除");
			throw new BusinessException("common:delete_error_exception",
					BusinessException.FAILURE);
			//		   return false;
		}

	}

	/**
	 * 是否允许登记验收期违规:做过复验后，前台会验证不让进入初验的违规登记，所以这里只处理复验时的违规登记,复验是只能修改那个通过状态的违规登记,但是此许可证id下已经有退还保证金了，也不能修改通过的了
	 * @author 许心炜
	 * @param abstractInpectionId - 抽象的验收ID
	 * @return Boolean
	 * @roseuid 493F871401C5
	 */
	private Boolean isAllowRegisterInspectionViolation(Long abstractInpectionId) {
		if (BlankUtil.isBlank(abstractInpectionId)) {
			throw new BusinessException("验收申请ID为空");
			//		   return false;
		}

		//	InspectionViolationDO inspectionViolationDO = inspectionViolationDao.getInspectionViolation(abstractInpectionId);

		ReinspectionDO reinspectionDO = reinspectionDao
				.findById(abstractInpectionId);

		//根据竣工复验获取验收申请
		InspectionRequisitionDO inspectionRequisitionDO = reinspectionDO
				.getInspectionRequisition();

		//验收状态，001-新建， 002-竣工初验， 003-复验不通过， 004-复验通过
		if ("003".equals(inspectionRequisitionDO.getRenovateLicence()
				.getRenovateRequisition().getProcessState())) {//装修进程处于验收期就行了。装修进程状态 001-申办，002-监管，003-验收，004-退保证金
			if (reinspectionDO != null) {//因为只是对复验进行
				if ("004".equals(reinspectionDO.getInspectionRequisition()
						.getInpectionState())) {
					//如果存在复验通过的状态，就只能修改“验收通过”的那条记录，并且不能新增。
					//验收结果 001-验收通过，002-验收不通过
					if ("001".equals(reinspectionDO.getResult())) {//inspectionViolationDO.getInspection().getResult()
						return true;
					} else {//存在复验通过的状态，不能修改“验收不通过”的那条记录
						throw new BusinessException(
								"renovate:inspection_reinspection_just_edit_passed",
								BusinessException.ERROR);
						//					   return false;
					}
				} else {//如果不存在复验通过的状态，可以新增和修改。
					return true;
				}
			} else {
				return false;
			}
		} else {
			throw new BusinessException(
					"renovate:inspection_reinspection_has_bail_records_not_del",
					BusinessException.ERROR);
		}
	}

	/**
	 * 通知新增了装修验收申请
	 * @author 许心炜
	 * @param licenceId - 许可证ID
	 * @roseuid 493F6686005D
	 */
	private void notifyAddInspectionRequisition(Long licenceId) {
		RenovateLicenceDO renovateLicenceDO = renovateLicenceDao
				.findById(licenceId);
		if (renovateLicenceDO != null) {
			renovateLicenceDO.getRenovateRequisition().setProcessState("003");
		}
	}

	/**
	 * 通知删除装修验收申请
	 * @author 许心炜
	 * @param inspectionRequisitionId - 验收装修验收申请ID
	 * @roseuid 493F7B75035B
	 */
	private void notifyDeleteInspectionRequisition(Long inspectionRequisitionId) {
		InspectionRequisitionDO inspectionRequisitionDO = inspectionRequisitionDao
				.findById(inspectionRequisitionId);

		if (inspectionRequisitionDO != null) {
			inspectionRequisitionDO.getRenovateLicence()
					.getRenovateRequisition().setProcessState("002");
		}

	}

	/**
	 * 通知竣工初验  002-竣工初验
	 * @author 许心炜
	 * @param inspectionRequisitionId - 装修验收申请ID
	 * @roseuid 493F849A00DA
	 */
	private void notifyFirstInspection(Long inspectionRequisitionId) {
		InspectionRequisitionDO inspectionRequisitionDO = inspectionRequisitionDao
				.findById(inspectionRequisitionId);
		//	   inspectionRequisitionDO.setInpectionState("002");
		inspectionRequisitionDO.setInpectionState("");
	}

	/**
	 * 通知新增了竣工复验
	 * @author 许心炜
	 * @param reinspectionId - 竣工复验ID
	 * @roseuid 493F8DE903B9
	 */
	private void notifyAddOrEditReinspection(Long reinspectionId) {
		ReinspectionDO reinspectionDO = reinspectionDao
				.findById(reinspectionId);
		//验收结果，001-验收通过，002-验收不通过;
		//验收状态，001-新建， 002-竣工初验， 003-复验不通过， 004-复验通过;
		if ("001".equals(reinspectionDO.getResult())) {
			reinspectionDO.getInspectionRequisition().setInpectionState("004");

			houseService.notifyFinishRenovation(reinspectionDO
					.getInspectionRequisition().getRenovateLicence()
					.getRenovateRequisition().getHouse().getKeyword());
			Date date = new Date();
			date = reinspectionDO.getInspectionDate();
			List<SysrefcodeDO> list = sysrefcodeService
					.querySysrefCodesByCode(SysrefcodeDO.RM_FEE_RETURN_CONFIG_CODE);
			if (list.size() > 0) {
				int addDate = new Integer(list.get(0).getValue()).intValue();
				reinspectionDO.getInspectionRequisition().setReturnFeeDate(
						DateUtil.addDate("dd", addDate, date));
			}
		}

		if ("002".equals(reinspectionDO.getResult())) {
			houseService.notifyHouseRenovationInAction(reinspectionDO
					.getInspectionRequisition().getRenovateLicence()
					.getRenovateRequisition().getHouse().getKeyword());
			reinspectionDO.getInspectionRequisition().setInpectionState("003");
			reinspectionDO.getInspectionRequisition().setReturnFeeDate(null);
		}
	}

	/**
	 * 通知删除竣工复验：
	 * @author 许心炜
	 * @param reinspectionId - 竣工复验ID
	 * @roseuid 493F91B30148
	 */
	private void notifyDeleteReinspection(Long reinspectionId) {
		//全部删除后,验收要改为验收状态， 002-竣工初验
		ReinspectionDO reinspectionDO = reinspectionDao
				.findById(reinspectionId);

		if (reinspectionDO != null) {
			List<ReinspectionDO> list = reinspectionDao
					.getReinspections(reinspectionDO.getInspectionRequisition()
							.getKeyword());
			//删除通过复验的记录，验收申请置为未通过状态
			if ("001".equals(reinspectionDO.getResult()) && list.size() > 1) {
				reinspectionDO.getInspectionRequisition().setInpectionState(
						"003");
				//通知改回装修中
				houseService.notifyHouseRenovationInAction(reinspectionDO
						.getInspectionRequisition().getRenovateLicence()
						.getRenovateRequisition().getHouse().getKeyword());
			} else if (list.size() == 1) {//还有一条记录并且删除后,验收要改为验收状态， 002-竣工初验
			//			   reinspectionDO.getInspectionRequisition().setInpectionState("002");
				if ("001".equals(reinspectionDO.getResult())) {//如果删除的这最后一条是通过状态的
					//通知改回装修中
					houseService.notifyHouseRenovationInAction(reinspectionDO
							.getInspectionRequisition().getRenovateLicence()
							.getRenovateRequisition().getHouse().getKeyword());
				}
				reinspectionDO.getInspectionRequisition().setInpectionState("");
			}

			reinspectionDO.getInspectionRequisition().setReturnFeeDate(null);
		}
	}

	/** 功能描述:  检查违规登记必填字段是否为空<br>
	 * @param inspectionViolationVO
	 * @return
	 * Create author:许心炜 <br>
	 * Create on:2009-1-6  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private Boolean checkRenovateViolationField(
			InspectionViolationVO inspectionViolationVO) {
		if (BlankUtil.isBlank(inspectionViolationVO.getCheckMan())
				|| inspectionViolationVO.getCheckDate() == null
				|| inspectionViolationVO.getViolateDate() == null
				|| BlankUtil.isBlank(inspectionViolationVO.getDescription())) {
			return false;
		}

		if (BlankUtil.isBlank(inspectionViolationVO.getAmerceYn())
				&& BlankUtil.isBlank(inspectionViolationVO.getOtherYn())
				&& BlankUtil.isBlank(inspectionViolationVO.getSeizureYn())
				&& BlankUtil.isBlank(inspectionViolationVO.getStopBehaviorYn())
				&& BlankUtil.isBlank(inspectionViolationVO.getStopRenovateYn())
				&& BlankUtil.isBlank(inspectionViolationVO.getTermReformYn())) {
			return false;
		}

		if (inspectionViolationVO.getCheckDate().before(
				inspectionViolationVO.getViolateDate())) {
			throw new BusinessException(
					"renovate:renovate_renovateviolation_checkdate_before_violationdate",
					BusinessException.ERROR);
		}

		return true;
	}

	/** 功能描述:  检查验收申请的必填字段是否为空<br>
	 * @param inspectionRequisitionVO
	 * @return
	 * Create author:许心炜 <br>
	 * Create on:2009-1-6  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private Boolean checkInspectionRequisitionField(
			InspectionRequisitionVO inspectionRequisitionVO) {
		if (inspectionRequisitionVO.getHouseNumber() == null
				|| inspectionRequisitionVO.getLinkManPhone() == null
				|| inspectionRequisitionVO.getFinishDate() == null
				|| inspectionRequisitionVO.getInspecteDate() == null) {
			return false;
		}

		return true;
	}

	/** 功能描述:检验初验或者复验字段是否为空  <br>
	 * @param abstractInspectionVO
	 * @return
	 * Create author:许心炜 <br>
	 * Create on:2009-1-6  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private Boolean checkReinspectionField(
			AbstractInspectionVO abstractInspectionVO) {
		if (abstractInspectionVO.getInspectionDate() == null
				|| abstractInspectionVO.getResult() == null
				|| abstractInspectionVO.getMembers() == null) {
			return false;
		}

		return true;
	}

	/** 功能描述: 获得指定格式的时间字符串，如果参数有一个为空，将返回null <br>
	 * @param pattern
	 * @param date
	 * @return
	 * Create author:许心炜 <br>
	 * Create on:2008-12-29  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private String getDateString(String pattern, Date date) {
		if (pattern == null || date == null) {
			return null;
		} else {
			SimpleDateFormat sf = new SimpleDateFormat(pattern);
			return sf.format(date);
		}
	}

	public void setReinspectionDao(IReinspectionDao reinspectionDao) {
		this.reinspectionDao = reinspectionDao;
	}

	public void setInspectionRequisitionDao(
			IInspectionRequisitionDao inspectionRequisitionDao) {
		this.inspectionRequisitionDao = inspectionRequisitionDao;
	}

	public void setRenovateLicenceDao(IRenovateLicenceDao renovateLicenceDao) {
		this.renovateLicenceDao = renovateLicenceDao;
	}

	public void setFirstInspectionDao(IFirstInspectionDao firstInspectionDao) {
		this.firstInspectionDao = firstInspectionDao;
	}

	public void setInspectionViolationDao(
			IInspectionViolationDao inspectionViolationDao) {
		this.inspectionViolationDao = inspectionViolationDao;
	}

	public void setRenovateViolationDao(
			IRenovateViolationDao renovateViolationDao) {
		this.renovateViolationDao = renovateViolationDao;
	}

	public void setOrganizationalStructureService(
			IOrganizationalStructureService organizationalStructureService) {
		this.organizationalStructureService = organizationalStructureService;
	}

	public void setSysrefcodeService(ISysrefcodeService sysrefcodeService) {
		this.sysrefcodeService = sysrefcodeService;
	}

	public void setHouseService(IHouseService houseService) {
		this.houseService = houseService;
	}

}
